﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChungNhanSinhVien.Entities;
using ChungNhanSinhVien.Infrastructures;
using ChungNhanSinhVien.Models;
using System.Collections.ObjectModel;

namespace ChungNhanSinhVien.Controllers
{
    public class YeuCauController : Controller
    {
        //
        // GET: /YeuCau/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GhiNhanYeuCau()
        {            
            if (SessionManager.user == null)
            {
                return RedirectToAction("UserDangNhap", "DangNhap");
            }
            else
            {
                LoaiChungNhanRepository loaiCNRepo = new LoaiChungNhanRepository();
                ChungNhanRepository chungnhanRepo = new ChungNhanRepository();

                YeuCauModel model = new YeuCauModel();
                model.msb_kiemtra = TempData["kiemtra"] as string ?? string.Empty;
                model.msb_taomoi = TempData["taomoi"] as string ?? string.Empty;

                model.loaiCNList = loaiCNRepo.LayTatCaLoaiChungNhanSelectList();
                if (model.loaiCNList != null)
                {
                    model.loaiCN = model.loaiCNList.FirstOrDefault().Value;
                    if (model.loaiCN != null)
                    {
                        model.chungnhanList = chungnhanRepo.LayChungNhansTheoLoaiCN(int.Parse(model.loaiCN));
                        if (model.chungnhanList != null)
                            model.chungnhan = model.chungnhanList.FirstOrDefault().Value;
                    }
                }
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult GhiNhanYeuCau(YeuCauModel model)
        {
            if (SessionManager.user == null)
            {
                return RedirectToAction("UserDangNhap", "DangNhap");
            }
            else
            {
                var action = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("LuuYeuCau"));
                if (action.Equals("LuuYeuCau"))
                {
                    string masv = SessionManager.user.MSSV;                    
                    YeuCauRepository yeucauRepo = new YeuCauRepository();
                    if (model.chungnhan != null)
                    {
                        #region Có yêu cầu cùng loại còn đang xử lý
                        if (yeucauRepo.KiemTraYeuCauDangXuLy(model, masv))
                        {
                            TempData["kiemtra"] = "yeucaudangxuly";
                        }
                        #endregion

                        #region Không có yêu cầu cùng loại còn đang xử lý
                        else
                        {
                            ChungNhanRepository chungnhanRepo = new ChungNhanRepository();
                            YeuCau yc = new YeuCau();
                            yc.StudentID = masv;
                            //yc.ChungNhanID = chungnhanRepo.LayChungNhanTheoTenChungNhan(model.chungnhan).ID;
                            yc.ChungNhanID = int.Parse(model.chungnhan);
                            yc.NgayYeuCau = DateTime.Now.Date;
                            yc.ThoiDiemYeuCau = DateTime.Now;
                            yc.TrangThai = "Chờ xử lý";
                            yeucauRepo.ThemYeuCau(yc);
                            TempData["taomoi"] = "thanhcong";
                        }
                        #endregion
                    }
                    else
                    {
                        TempData["kiemtra"] = "chungnhannull";
                    }
                }
                return RedirectToAction("GhiNhanYeuCau");
            }
        }

        public ActionResult XemKetQuaGhiNhan(YeuCauModel model)
        {
            if (SessionManager.user == null)
            {
                return RedirectToAction("UserDangNhap", "DangNhap");
            }
            else
            {
                ViewData["masv"] = SessionManager.user.MSSV;
                return View();
            }
        }

        [ChildActionOnly]
        public ActionResult UC_ThongTinSinhVien()
        {
            return PartialView();
        }

        [ChildActionOnly]
        public ActionResult UC_XemKetQuaGhiNhan(string studentID)
        {
            ViewData["tb"] = TempData["tb"] as string ?? string.Empty;
            if (studentID != null)
            {
                ViewData["CoDuLieuTrongList"] = "";
                YeuCauRepository ycRepo = new YeuCauRepository();
                var list = ycRepo.LayYeuCausTheoMaSV(studentID);
                if (list.Count != 0)
                {
                    ViewData["CoDuLieuTrongList"] = "co";
                    return PartialView(list);
                }
                else
                {
                    ViewData["CoDuLieuTrongList"] = "khong";
                    return PartialView(list);
                }
            }
            else
            {
                return RedirectToAction("UserDangNhap", "DangNhap");
            }
        }

        public ActionResult HuyYeuCau(int id)
        {
            TempData["tb"] = "";
            if (SessionManager.user == null)
            {
                return RedirectToAction("UserDangNhap", "DangNhap");
            }
            else
            {
                if (new YeuCauRepository().Xoa(id))
                {
                    TempData["tb"] = "xoathanhcong";
                }
                else
                {
                    TempData["tb"] = "xoathatbai";
                }
                return RedirectToAction("XemKetQuaGhiNhan");
            }            
        }


        public JsonResult LoadChungNhan(int loaiCN)
        {
            if (loaiCN != 0)
            {
                ChungNhanRepository chungnhanRepo = new ChungNhanRepository();
                List<SelectListItem> result = chungnhanRepo.LayChungNhansTheoLoaiCN(loaiCN);
                return Json(result);
            }
            return new JsonResult();
        }

        public ActionResult XemKetQuaYeuCau()
        {

            return View();
        }

    }
}
